package com.zhongwang.mms.module.material.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.MProjectInventoryMark;
import com.zhongwang.mms.module.material.dao.MProjectInventoryMarkDao;
import com.zhongwang.mms.module.material.model.MProjectInventoryMarkModel;
import com.zhongwang.mms.module.material.service.MProToItyService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 项目划库存 控制层
 * @Author: China.wsg
 * @Date: 2020/04/23 15:52
 */
@Controller
@RequestMapping("/material/proToIty")
public class MProToItyController extends BaseController {

    @Autowired
    private MProjectInventoryMarkDao projectInventoryMarkDao;
    @Autowired
    private MProToItyService proToItyService;

    /**
     * 项目划库存标记列表 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"matinv:matprowar:view"})
    public String page(Model model) {

        model.addAttribute("userName", ShiroUtils.getUser().getName());
        return "material/matWarManage/ProToIty/list";
    }

    /**
     * 查询项目划库存标记列表
     *
     * @param projectInventoryMarkModel 查询条件
     * @return
     */
    @ResponseBody
    @PostMapping("/markList")
    public JsonResult markList(MProjectInventoryMarkModel projectInventoryMarkModel) {
        startPage();
        List<MProjectInventoryMarkModel> list = projectInventoryMarkDao.findMarkListByQuery(projectInventoryMarkModel);
        return jsonResult(list);
    }

    /**
     * 保存项目划库存标记
     *
     * @param projectInventoryMark 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"matinv:matprowar:edit"})
    public JsonResult save(MProjectInventoryMark projectInventoryMark) {
        JsonResult jsonResult = new JsonResult();
        String msg = proToItyService.save(projectInventoryMark);
        if (!msg.contains("标记量大于可用量")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 保存项目划库存标记（批量）
     *
     * @param arr 标记Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/togetherMark")
    @RequiresPermissions({"matinv:matprowar:edit"})
    public JsonResult togetherMark(@RequestBody List<Integer> arr) {

        proToItyService.togetherMark(arr);
        return success();
    }

    /**
     * 修改项目划库存标记状态
     *
     * @param projectInventoryMark 修改信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/updateStatus")
    @RequiresPermissions(value = {"matinv:matprowar:edit","matinv:matprowarexec:edit"}, logical = Logical.OR)
    public JsonResult updateStatus(MProjectInventoryMark projectInventoryMark) {
        return jsonResult(proToItyService.updateStatus(projectInventoryMark));
    }

    /**
     * 修改项目划库存标记
     *
     * @param projectInventoryMark 保存信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"matinv:matprowar:edit"})
    public JsonResult edit(MProjectInventoryMark projectInventoryMark) {
        JsonResult jsonResult = new JsonResult();
        String msg = proToItyService.edit(projectInventoryMark);
        if (!msg.contains("标记量大于可用量")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 项目划库存批量提交
     *
     * @param arr 标记Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/togetherCommit")
    @RequiresPermissions({"matinv:matprowar:edit"})
    public JsonResult togetherCommit(@RequestBody List<Integer> arr) {

        proToItyService.togetherCommit(arr);
        return success();
    }

    /**
     * 项目划库存执行 页面
     *
     * @return 页面路径
     */
    @GetMapping("/check")
    @RequiresPermissions({"matinv:matprowarexec:view"})
    public String check() {

        return "material/matWarManage/ProToIty/check";
    }

    /**
     * 项目划库存执行（单条）
     *
     * @param pimId
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkSingle")
    @RequiresPermissions({"matinv:matprowarexec:edit"})
    public JsonResult checkSingle(Integer pimId) {
        JsonResult jsonResult = new JsonResult();
        String msg = proToItyService.checkSingle(pimId);
        if (!msg.contains("项目划库存执行失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 项目划库存执行（批量执行）
     *
     * @param arr 明细Id集合
     * @return
     */
    @ResponseBody
    @PostMapping("/checkTogether")
    @RequiresPermissions({"matinv:matprowarexec:edit"})
    public JsonResult checkTogether(@RequestBody List<Integer> arr) {

        JsonResult jsonResult = new JsonResult();
        String msg = proToItyService.checkTogether(arr);
        if (!msg.contains("项目划库存执行失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 项目划库存撤销（单条）
     *
     * @param pimId
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/cancelSingle")
    @RequiresPermissions({"matinv:matprowarexec:edit"})
    public JsonResult cancelSingle(Integer pimId) {
        JsonResult jsonResult = new JsonResult();
        String msg = proToItyService.cancelSingle(pimId);
        if (!msg.contains("项目划库存撤销失败")) {
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
        }
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /**
     * 列表 导出
     * @param projectInventoryMarkModel 查询条件
     * @return list
     */
    @GetMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(HttpServletResponse response, MProjectInventoryMarkModel projectInventoryMarkModel) {
        proToItyService.exportExcel(response,projectInventoryMarkModel);
    }

}
